
<template>
  <el-dialog v-model="isShowDialog"
             :before-close="handleClose"
             :title="title"
             draggable
             width="500px"
  >
    <el-form  ref="formRef" :model="saveParams" label-width="100px" :rules="rules">
      <el-form-item label="属性名称" prop="name">
        <el-input v-model="saveParams.attrName"  clearable/>
      </el-form-item>
      <el-form-item label="属性值" prop="rule">
        <el-select
            style="width: 100%"
            v-model="saveParams.rule"
            multiple
            filterable
            allow-create
            default-first-option
            :reserve-keyword="false"
        >
        </el-select>
      </el-form-item>
      <el-form-item label="所属仓库" prop="warehouseId">
        <el-select
            :disabled="saveParams.attrId"
            clearable
            v-model="saveParams.warehouseId"
        >
          <template v-for="(item,index) in warehouseList" :key="index">
            <el-option :label="item.warehouseName" :value="item.warehouseId"></el-option>
          </template>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="clickSave"> 确定 </el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {getCurrentInstance, onMounted, ref} from "vue";
import {skuAttrSave} from "@/api/scApi";
import {getCompanySelect, getWarehouseSelect} from "@/api/system/user";


const {proxy} = getCurrentInstance();
const props = defineProps({
  isShowDialog: {
    type: Boolean,
    default: false
  },
  title: {type: String},
  rowData:{type:Object}, //编辑用
})

onMounted(()=>{
  getWarehouse().then(()=>{
    if(JSON.stringify(props.rowData) !='{}'){
      //编辑
      saveParams.value=JSON.parse(JSON.stringify(props.rowData))
    }
  })
})

const emits = defineEmits(['cancel'])
function handleClose() {
  emits('cancel')
}
let saveParams=ref({})
const rules = ref({
  attrName: [{required: true, message: "属性名称不能为空", trigger: "blur"}],
  rule: [{required: true, message: "属性值不能为空", trigger: "blur"}],
  warehouseId: [{required: true, message: "所属仓库不能为空", trigger: "change"}],
});

function clickSave(){
  saveParams.value.rule = saveParams.value.rule.join(",")
  proxy.$refs.formRef.validate((valid, fileds) => {
    if (valid) {
      skuAttrSave(saveParams.value).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess('保存成功')
          emits('cancel', true)
        }
      })
    }
  })
}
const sysUserInfo=JSON.parse(localStorage.getItem("userInfo"))
//获取仓库下拉数据
let warehouseList = ref([])
function getWarehouse(){
  return new Promise((resolve, reject) => {
    getWarehouseSelect({'companyId': sysUserInfo.companyId}).then(res => {
      warehouseList.value = res.data
      resolve()
    })
  })
}
</script>

<style scoped lang="scss">

</style>